package com.jule.miaozhuapc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/*时间格式化*/
public class DateFormat {

	/**
	 * Date 转为 "2016-12-28 12:53:46.000+0800"
	 * @param date
	 * @return
	 */
	public String getDate(Date date) {
		if (date == null)
			return "";
		try {
			if (date != null) {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
				return fmt.format(date);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date + "";
	}

	/**
	 * Date 转为 "2016.12.28"
	 * @param date
	 * @return
	 */
	public String getDianDate(Date date) {
		if (date == null)
			return "";
		try {
			if (date != null) {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy.MM.dd");
				return fmt.format(date);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date + "";
	}
	/**
	 * Date 转为 "2016-12-28 "
	 * @param date
	 * @return
	 */
	public String getIndexDate(Date date) {
		if (date == null)
			return "";
		try {
			if (date != null) {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
				return fmt.format(date);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date + "";
	}

	/**
	 * 
	 * @param date
	 * @return
	 */
	public Date getAddTime(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 字符转Date
	 * 
	 * @param dateString
	 * @return
	 */
	public Date judgeDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(dateString);
			return date;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	/**
	 * 有出生年月 计算出 年龄
	 * 
	 * @param dateString
	 * @return
	 */
	public Long getAge(String dateString) {
		if(dateString == null || ("null").equals(dateString)) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(dateString);

			Date now = new Date();
			Long age = (long) (now.getYear() - date.getYear());
			return age;
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}

		return null;
	}

	/**
	 * 返回yyyy-MM-dd格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public Date getNYR(Date date) {
		try {
			if (date != null) {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
				return fmt.parse(fmt.format(date));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回yyyyMMdd格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public String getNewNYR() {
		try {
				SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
				return fmt.format(new Date());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "20171204";
	}
	/**
	 *
	 *  
	 * */
	/**
	 * 入参时间 判断距离现在时间的纬度 1分钟类刚刚； 1-59 分钟； 1-24小时; 天
	 * 
	 * @param strDate
	 * @return
	 */
	public String getDateLimit(String strDate) {
		long time = new Date().getTime();// 当前时间

		String howLong = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(strDate);// 入参转时间
			long addtime = date.getTime();

			long timer = time - addtime;// 时间差
			if (timer < (60 * 1000)) {
				howLong = "刚刚";
			} else if (timer > (60 * 1000) && timer < (60 * 60 * 1000)) {
				howLong = (int) (timer / (1000 * 60)) + "分钟前";
			} else if (timer > (60 * 60 * 1000) && timer < (24 * 60 * 60 * 1000)) {
				howLong = (int) (timer / (1000 * 60 * 60)) + "小时前";
			} else if (timer > (24 * 60 * 60 * 1000)) {
				// howLong = (int) (timer/(1000 * 60 * 60 * 24))+"天前";改为
				// 时间“20170508”
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
				howLong = sdf2.format(date);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return howLong;
	}

	/**
	 * 获取当前年月日
	 * 
	 * @return
	 */
	public Date[] getCurrent() {
		Date[] dates = new Date[2];
		try {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			//System.out.println("旧" + new Date());
			// 今天
			dates[0] = fmt.parse(fmt.format(new Date()));
			// 明天
			dates[1] = fmt.parse(fmt.format(new Date().getTime() + 1000 * 60 * 60 * 24));
			//System.out.println(dates[0] + " /" + dates[1]);
			return dates;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dates;
	}

	/**
	 * 将年纪转为时间
	 * 
	 * @param age
	 * @return
	 */
	public String ageChangeDate(int age) {

		Calendar now = Calendar.getInstance();
		Date now2 = new Date();
		now.set(Calendar.YEAR, now.get(Calendar.YEAR) - age);
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd 00:00:00.0000");
		//System.out.println(" age = " + age + " date = " + fmt.format(now.getTime()));
		return fmt.format(now.getTime());
	}

	/**
	 *  
	 * @return 2017-06-01
	 */
	/**
	 * 获取从当前算起 后续size月 每月第一天时间
	 * @param size 几个月
	 * @return  Date[]
	 * @throws Exception
	 */
	public Date[] getMonthStart(int  size) throws Exception{
		Calendar now = Calendar.getInstance();
		now.set(Calendar.DAY_OF_MONTH, 1);     //设置到每月的第一天

		//now.add(Calendar.DAY_OF_MONTH, -now.get(Calendar.DAY_OF_MONTH)+1);
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		
		Date[] dates = new Date[size];
		int  i = 0;
		while(i < size){
			dates[i] = fmt.parse( fmt.format(now.getTime()));
			now.add(Calendar.MONTH, 1);
			i++;
		}		
		for (Date date : dates) {
			//System.out.println(" 时间 =" + fmt.format(date));
		}
		return  dates;	
	}
	
	public static void main(String[] args)throws Exception {

		/*
		 * String age = getDateLimit("2017-03-9 11:45:25");
		 */ // System.out.print(getCurrent());
		//System.out.println(new DateFormat().getDateLimit("2017-03-9 11:45:25"));
		//System.out.println(new DateFormat().getMonthStart(3));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		//System.out.print(  calendar.getTime().getTime() > new Date().getTime() );
		System.out.println(new DateFormat().getNewNYR());
	}
}
